//
//  AppCoordinator.swift
//  SwiftUi
//
//  Created by xm on 05/09/2025.
//

import Foundation
import SwiftUI

//var routes: [Route] = []
@MainActor
final class AppCoordinator: ObservableObject {
    @Published
    var tab: Tab = .movies
 
    private lazy var moviesCoordinator = MoviesCoordinator()
    private lazy var animesCoordinator = AnimesCoordinator()
    private lazy var moviesViewModel = MoviesViewModel(coordinator: moviesCoordinator)
    private lazy var animesViewModel = AnimesViewModel(coordinator: animesCoordinator)

    
    func makeMoviesView() -> some View {
        MoviesNavigationView(coordinator: moviesCoordinator, viewModel: moviesViewModel)
            .tabItem {
                tabbarItem(text: "Movies", image: "film.stack")
            }
    }

    func makeAnimeView() -> some View {
        AnimesNavigationView(coordinator: animesCoordinator, viewModel: animesViewModel)
            .tabItem {
                tabbarItem(text: "Anime", image: "sparkles.rectangle.stack")
            }
    }

    private func tabbarItem(text: String, image: String) -> some View {
        VStack {
            Image(systemName: image)
                .imageScale(.large)
            Text(text)
        }
    }
    
}

extension AppCoordinator {
    enum Tab {
        case movies
        case anime
    }
}


struct RouteIdentifier<T>: Hashable {
    let value: T
    let id: AnyHashable
    
    init<ID: Hashable>(value: T, id: (T) -> ID) {
        self.value = value
        self.id = AnyHashable(id(value))
    }
    
    func hash(into hasher: inout Hasher) {
        hasher.combine(id)
    }
    
    static func == (lhs: RouteIdentifier<T>, rhs: RouteIdentifier<T>) -> Bool {
        return lhs.id == rhs.id
    }
}

